<template>
	<view>
		<view class="goods-item" v-for="(item, i) in goodsList" :key="i" @click="gotoGoodsDetail(item.goods_id)">
			<view class="left">
				<image :src="item.goods_big_logo || defaultPic" class="goodspic"></image>
			</view>
			<view class="right">
				<text class="goodsname">{{item.goods_name}}</text>
				<text class="goodsprice">￥{{item.goods_price.toFixed(2)}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
				queryObj: {
				      // 查询关键词
				      query: '',
				      // 商品分类Id
				      cid: '',
				      // 页码值
				      pagenum: 1,
				      // 每页显示多少条数据
				      pagesize: 10
				},
				total: 0,
				isLoading: false,
				goodsList: []
			};
		},
		methods: {
			async getGoodsList(cb) {
				this.isLoading = true
				const { data: res } =await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
				cb && cb()
				if(res.meta.status !== 200) return uni.$showMsg()
				this.goodsList = [...this.goodsList, ...res.message.goods]
				this.total = res.message.total
				this.isLoading = false
			},
			gotoGoodsDetail(id) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + id
				})
			}
		},
		onLoad(option) {
			this.queryObj.query = option.query || ''
			this.queryObj.cid = option.cid || ''
			this.getGoodsList()
		},
		onReachBottom() {
			if(this.queryObj.pagenum*this.queryObj.pagesize > this.total) return uni.$showMsg('没有更多了')
			this.queryObj.pagenum++
			this.getGoodsList()
			
		},
		onPullDownRefresh() {
			this.queryObj.pagenum = 1
			this.isLoading = false
			this.total = 0
			this.goodsList = []
			this.getGoodsList(() => uni.stopPullDownRefresh())
		}
	}
</script>

<style lang="scss">
.goods-item {
	margin: 7px;
	display: flex;
	border-bottom: 1px solid #f0f0f0;
	.left {
		.goodspic {
			height: 100px;
			width: 100px;
			margin-right: 10px;
			object-fit: contain;
		}
	}
	.right {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		.goodsname {
			font-size: 13px;
			margin-top: 5px;
		}
		.goodsprice {
			font-size: 16px;
			margin-bottom: 5px;
			color: #c00000;
		}
	}
}
</style>
